﻿#include "force_flag.h"

static bank3 char gForceFlag[CHANNEL_NUM];
bank1 unsigned char gTri_Times[CHANNEL_NUM];
bank1 unsigned char gRel_Times[CHANNEL_NUM];
bank1 unsigned char gRel_Times_Pre[CHANNEL_NUM]; 
extern bank3 int gForceData_Peak[CHANNEL_NUM];
extern bank3 int gForceData_Vally[CHANNEL_NUM];;
extern bank1 int gForceData_Peak_Pre[CHANNEL_NUM];
extern bank1 int gForceData_Vally_Pre[CHANNEL_NUM];
char Get_Forceflag(char ch_num)
{
	return gForceFlag[ch_num];
}
/**
 * @brief force_flag判断
 * 
 */
void Force_Flag_Judeg(char ch_num)
{
	int temp;
	char i;
	char pos_rule,neg_rule;
	pos_rule = 0;
	neg_rule = 0;
	if(gForceFlag[ch_num] == NO_TOUCH)	// 无触摸	
	{
		//正向 
		if(Get_ForceData(ch_num) > FORCE_FLAG_TIR_LEVEL)
		{
			pos_rule |= 0x01;
		}
		if(gRawdata_Sp[ch_num] >RAWDATA_SP_TRI)
		{
			pos_rule |= 0x02;
		}
		//先有负信号，然后马上出现正信号
//		if(	Get_ForceData(ch_num) > -gForceData_Vally_Pre[ch_num]>>1)
//		{
//			pos_rule |= 0x04;
//		}
//		//正正   根据业务代码判断是否增加
//		if(Get_ForceData() > gForceData_Peak_Pre>>2)
//		{
//			pos_rule |= 0x08;
//		}
		
		//负向
		if(Get_ForceData(ch_num) < -FORCE_FLAG_TIR_LEVEL)
		{
			neg_rule |= 0x01;
		}
		if(gRawdata_Sp[ch_num] <-RAWDATA_SP_TRI)
		{
			neg_rule |= 0x02;
		}
//		//先有正信号，然后马上出现负信号
//		if(Get_ForceData(ch_num) < -gForceData_Peak_Pre[ch_num]>>2||gRel_Times[ch_num]>20)
//		{
//			neg_rule |= 0x04;
//		}
//		
		
		//判断
		if(	pos_rule == 0x03)
		{
			gForceFlag[ch_num] = POS_TOUCH;
		}
		if(	neg_rule == 0x03)
		{
			gForceFlag[ch_num] = NEG_TOUCH;
		}
	
	}else if(gForceFlag[ch_num] == POS_TOUCH)//正信号离手判断
	{	
		temp = gForceData_Peak[ch_num] * 3/10;
		if(Get_ForceData(ch_num) < temp || Get_ForceData(ch_num) < FORCE_FLAG_REL_LEVEL)  //小于峰值的0.3
		{
			gForceFlag[ch_num] = NO_TOUCH;
		}
		
	}else if(gForceFlag[ch_num] == NEG_TOUCH)//负信号离手判断
	{
		temp = gForceData_Vally[ch_num] * 3/10;
		if(Get_ForceData(ch_num) > temp || Get_ForceData(ch_num) > -FORCE_FLAG_REL_LEVEL)  //小于峰值的0.3
		{
			gForceFlag[ch_num] = NO_TOUCH;
		}
	}	

}
/**
 * @brief 记录时间
 * 
 */
void Record_Time_process(char ch_num)
{
	char i;

	if(gForceFlag[ch_num])
	{
		if(gTri_Times[ch_num]<200)
		{
			gTri_Times[ch_num]++;
			
			//记录上一次离手时间，根据业务代码的需求使用
			if(gTri_Times[ch_num] == 1)
			{
				gRel_Times_Pre[ch_num] = gRel_Times[ch_num];
			}
			
		}
		gRel_Times[ch_num] = 0;
	}else{
		if(gRel_Times[ch_num]<200)
		{
			gRel_Times[ch_num]++;	
		}
		gTri_Times[ch_num] = 0;
	}	
}
/**
 * @brief force_flag判断，和对应时间记录
 * 
 */
void Force_Flag_Process()
{
	char i;
	
	for(i = 0 ;i<CHANNEL_NUM;i++)
	{
		//判断force_flag
		Force_Flag_Judeg(i);
		
		//记录按压状态的时间
		Record_Time_process(i);
	}

}